package com.fyyg.admin.util;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.fyyg.common.util.HttpTookit;




public class PageEncodingFilter   implements Filter {

	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		
//		HttpServletResponse response = (HttpServletResponse) res;
//		HttpSession session = request.getSession();
		
		String adminLogin = "-";
		String loginName = (String)request.getSession().getAttribute("UID");
		if (loginName != null && !"".equals(loginName)){
			adminLogin = loginName;
		}
		
		String ip=HttpTookit.getRealIpAddr(request);
		
		//============java 只是用来生成个时间 ================
		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
		int year = c.get(Calendar.YEAR); 
		int month = c.get(Calendar.MONTH)+1; 
		int date = c.get(Calendar.DATE); 
		int hour = c.get(Calendar.HOUR_OF_DAY); 
		int minute = c.get(Calendar.MINUTE); 
		int second = c.get(Calendar.SECOND); 
		String timeNow = year + "-" + month + "-" + date + " " +hour + ":" +minute + ":" + second;
		//============java  只是用来生成个时间  ================
		
	    //============java 获取请求 URL     ================
		String   url  = request.getScheme()+"://"; //请求协议 http 或 https    
		url+=request.getHeader("host");  // 请求服务器    
		url+=request.getRequestURI();     // 工程名      
		if(request.getQueryString()!=null) //判断请求参数是否为空  
		url+="?"+request.getQueryString();   // 参数   
		System.out.println("["+timeNow+"] ["+ip+"]["+adminLogin+"]"+url);   
		req.setAttribute("weixinUrl", url);

		String path = request.getServletPath();
		String contextPath = request.getContextPath();
		req.setCharacterEncoding("utf-8");

		if(
				(path.indexOf("/index") != -1)||
				(path.indexOf("/login") != -1)||
				(path.indexOf("/package") != -1)||
				(path.indexOf("/imageupload") != -1)||
				(path.indexOf("/order") != -1)||
				(path.indexOf("/product") != -1)
			
			
		){
			chain.doFilter(req, res);
		}
		
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}}
